package com.project.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.bean.ClassBean;
import com.project.bean.StudentBean;
import com.project.mapper.IClassMapper;
import com.project.mapper.IStudentMapper;
import com.project.service.IClassService;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
@Service
@Transactional
public class ClassServiceImp implements IClassService {
    @Autowired
    private IClassMapper classMapper;
    @Autowired
    private IStudentMapper studentMapper;
    @Override
    public ArrayList<ClassBean> showAllClass() {
      return (ArrayList<ClassBean>) classMapper.selectList(null);
    }

    @Override
    public boolean addClass(ClassBean classBean) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("c_name",classBean.getClassName());
        ClassBean classBean1 = classMapper.selectOne(qw);
        if(classBean1==null){
            classMapper.insert(classBean);
            return true;
        }
        return false;
    }

    @Override
    public boolean delClass(Integer classId) {
        QueryWrapper<StudentBean> qw = new QueryWrapper<>();
        qw.eq("fk_classid",classId);
        int num =studentMapper.selectCount(qw);
        if(num==0){
            classMapper.deleteById(classId);
            return true;
        }
        return false;
    }
}
